extends ../layout

block vars
  - var title = topic.title

block scripts
  if locals.runEnv == 'development'
    script(src="/javascripts/jquery-1.10.2.js")
  if locals.runEnv == 'production'
    script(src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js")

  script.
    $(document).ready(function(){
      // 处理csrf token 
      $('')


      $('.ui-footer').on('click', '.J_topicFavorite', function(e) {
        var _node = $(this);
        var _type = $(this).attr('data-type');
        var _errorMap = {
          topic_not_exist: '主题不存在',
          can_not_favorite_your_topic: '不能收藏自己的主题',
          already_favorited: '已经收藏过了',
          please_signin: '请登录',
        };
        e.preventDefault();
        $.ajax({
          type: "POST",
          url: _node.attr('href'),
          beforeSend: function(req) {
            req.setRequestHeader('X-CSRF-Token', "#{csrf_token}")
          },
          success: function(data) {
            if(data.success) {
              if(_type == 'favorite'){
                _node.replaceWith("<a href='/topics/#{topic.id}/unfavorite' data-type='unfavorite' class='J_topicFavorite'>收藏成功,点击取消</a>");
              } else {
                _node.replaceWith("<a href='/topics/#{topic.id}/favorite' data-type='favorite' class='J_topicFavorite'>取消成功,点击收藏</a>");
              }
            } else {
              if(data.message == "already_favorited"){
                _node.replaceWith("<a href='/topics/#{topic.id}/unfavorite' data-type='unfavorite' class='J_topicFavorite'>已经收藏过，取消收藏</a>");
              } else {
                _node.html('<span>' + _errorMap[data.message] + '</span>');
              }
            }
          }
        });
      });

      $('.J_topicVote').click(function(e) {
          var _node = $(this);
          var _errorMap = {
            topic_not_exist: '主题不存在',
            can_not_vote_your_topic: '不能喜欢自己的主题',
            already_voted: '感谢已经表示过',
            please_signin: '请登录',
          };
          e.preventDefault();
          $.ajax({
            type: "POST",
            url: _node.attr('href'),
            beforeSend: function(req) {
              req.setRequestHeader('X-CSRF-Token', "#{csrf_token}")
            },
            success: function(data) {
              if(data.success) {
                _node.html('<span class="f11 gray" style="text-shadow: 0px 1px 0px #fff;">感谢已表示</span>');
              } else {
                _node.html('<span>' + _errorMap[data.message] + '</span>');
              }
            }
          });
        });

      $('.J_replyTo').click(function(e) {
        username = $(this).attr('data-username');
        index = $(this).attr('data-index');
        replyContent = $('.J_replyContent');
        oldContent = replyContent.val();
        prefix = "#" + index + "楼 " + "@" + username + " ";
        newContent = '';

        if(oldContent.length > 0){
          if (oldContent != prefix) {
            newContent = oldContent + "\n" + prefix;
          }
        } else {
            newContent = prefix;
        }
        replyContent.focus();
        replyContent.val(newContent);
        });

        $('.J_replyContent').keydown(function(e) {
          if((e.ctrlKey || e.metaKey) && e.which === 13) {
            e.preventDefault();
            $(e.currentTarget).parent('form').submit();
          }
        });
    });

block content
  .container-fluid.container.mt15
    .row-fluid
      .span9.left
        .topic-detail.container-box
          .ui-header
            a(href='/u/#{topic.user.username}')
              img.avatar(src='#{topic.user.avatarUrl()}', alt='')
            .main
              h3.title= topic.title
              .meta
                span.node
                  a(href='/nodes/#{topic.node.key}')= topic.node.name  
                |  • 
                span.username
                  a(href='/u/#{topic.user.username}')= topic.user.username 
                |  •  
                span.created-time 发表于#{locals.humanDatetime(topic.created_at)}    
                if topic.replies_count != 0
                  |  •  
                  span.last-reply-username
                    | 最后回复来自 
                    a(href='/u/#{topic.last_replied_by}')
                      strong=topic.last_replied_by 
                  |  •  
                  span.last-reply-time= locals.humanDatetime(topic.last_replied_at)
          .ui-content
            p!= topic.content_html
          .ui-footer
            a.J_topicFavorite(href='/topics/#{topic.id}/favorite', data-type='favorite') 加入收藏
            a(href='#', onclick='window.open("http://service.weibo.com/share/share.php?url=http://#{domain_name}/topics/#{topic.id}&title=#{topic.title}", "_blank", "width=550, height=370");') 新浪微博
            a.J_topicVote(href='/topics/#{topic.id}/vote') 喜欢
            span.hits.fr.mr10 #{topic.hit} 次点击
            span.fr.mr10 #{topic.vote_users.length} 个喜欢
        if topic.replies.length == 0
          .alert.mt20.no-replies 暂无回复，说出你的观点吧
        unless topic.replies.length == 0
          .topic-reply.container-box.mt10
            .ui-header
              span共收到#{topic.replies.length}条回复
            .ui-content
              for reply, i in topic.replies
                .reply-item
                  a(href='/u/#{reply.user.username}')
                    img.avatar(src='#{reply.user.avatarUrl()}', alt='')
                  .main
                    .meta
                      a.reply-username(href='/u/#{reply.user.username}')
                        span.username= reply.user.username
                      span.time= locals.humanDatetime(reply.created_at)
                      span.floor.fr ##{i+1}
                      span.reply-to.fr.J_replyTo(data-username='#{reply.user.username}', data-index="#{i+1}")
                        img(src='/images/reply.png', alt='')
                    span.content
                      p!= reply.content_html
            .ui-footer
        .topic-reply-create.container-box.mt10
          .ui-header
            span.title 创建新的回复
          .ui-content
            if locals.current_user
              form.mt10(action='/topics/#{topic.id}/replies', method='post')
                if notices && notices.length != 0
                  ul.alert.errors.alert-error
                    for notice in notices
                      li= notice
                textarea.content.mt5.smart-code-support.J_replyContent(rows='3', placeholder='回复内容', name='content')
                input#csrf(type='hidden', name='_csrf', value="#{locals.csrf_token}")
                input#checkall.btn.btn-small(type='submit', value='立即回复')
            else
              a.btn.btn-small.mt5.mr10(type='button', href='/login') 登录
              a.btn.btn-small.mt5(type='button', href='/register') 注册
      .span3.right
        if locals.current_user
          - var user = locals.current_user
          include ../share/user_info
        else 
          include ../share/login_box
